//
//  BackProgressView.m
//  Progress
//
//  Created by liang on 16/9/5.
//  Copyright © 2016年 liang. All rights reserved.
//

#import "BackProgressView.h"
#import "UIViewExt.h"
@interface BackProgressView()
@property (nonatomic,strong) UIView *backV;
@property (nonatomic,strong) UILabel *progressNumLb;
@end
@implementation BackProgressView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        _backV = [[UIView  alloc] initWithFrame:self.bounds];
        _backV.backgroundColor =[UIColor colorWithWhite:0.5 alpha:0.5];
        [self addSubview: _backV];
        
        _progressNumLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        _progressNumLb.backgroundColor = [UIColor clearColor];
        _progressNumLb.textColor = [UIColor whiteColor];
        _progressNumLb.textAlignment = NSTextAlignmentCenter;
        _progressNumLb.text = [NSString stringWithFormat:@"%d%%",0];

        [self addSubview:_progressNumLb];
    }
    return self;
}

- (void)setProgress:(float)progress
{
    if (progress > 99 && progress < 100) {
        progress = 99;//这样圆圈会留一个小缺口，看得出还没满。如果是99.x的话，圆圈已经被画满了。
    }
    if (progress < 1 && progress > 0) {//百分之零点几时，为了避免界面上数字是0%，所以取上限即让它等于1%
        progress = 1;
    }
    _progress = progress / 100;
    _progressNumLb.text = [NSString stringWithFormat:@"%ld%%",(long)progress];
    _backV.height = (1-_progress)*self.height;
    _backV.top = _progress*self.height;
    
    if (_progress == 1) {
        [self removeFromSuperview];
    }
    
}
@end
